package com.labManage.controller;

import com.labManage.dao.BorrowInfoDao;
import com.labManage.dao.GoodsDao;
import com.labManage.dao.RoleDao;
import com.labManage.dao.UserDao;
import com.labManage.entity.BorrowInfo;
import com.labManage.entity.Role;
import com.labManage.entity.User;
import com.labManage.service.BorrowService;
import com.labManage.vo.BorrowInfoVo;
import com.labManage.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/borrow")
public class BorrowController {


    @Autowired
   BorrowService borrowInfoService;

    @Autowired
    BorrowInfoDao borrowInfoDao;

    @Autowired
    UserDao userDao;

    @Autowired
    GoodsDao goodsDao;

    @Autowired
    RoleDao roleDao;
    @RequestMapping("/getBorrowInfo")
    @ResponseBody
    public ResultVo getBorrowInfo(Integer page,Integer limit,String borrowPersonNo){
        List<BorrowInfoVo> borrowInfos = borrowInfoService.getBorrowInfo(page, limit,borrowPersonNo);
        Long resultNum = borrowInfoService.getBorrowInfoNum(borrowPersonNo);
        int length = 0;
        if(resultNum>0){
            length = resultNum.intValue();
        }
        if(null!=borrowInfos){
            return ResultVo.build(length,0,"",borrowInfos);
        }else{
            return ResultVo.build(201,"",null);
        }
    }
    @RequestMapping("/getBorrowNum")
    @ResponseBody
    public ResultVo getBorrowNum(){
        long borrowNum = borrowInfoDao.selectBorrowNum();
        return ResultVo.build(0,"",borrowNum);
    }

    @RequestMapping("/borrowHandleState")
    @ResponseBody
    @RequiresPermissions("borrow:borrow")
    public ResultVo borrowHandleState(Long borrowId){
        if(null!=borrowId){
            int result = borrowInfoService.borrowHandleState(borrowId);
            if(0==result){
                return ResultVo.build(0,"操作成功");
            }else if(1==result) {
                return ResultVo.build(201,"操作失败");
            }
            else if(2==result) {
                return ResultVo.build(201,"非法访问");
            }
            else if(3==result) {
                return ResultVo.build(500,"服务器异常");
            }else if(4==result){
                return ResultVo.build(201,"库存不足");
            }else{
                return ResultVo.build(500,"服务器异常");
            }
        }else{
            return ResultVo.build(201,"非法访问");
        }
    }

    @RequestMapping("/getRecentlyBorrowInfo")
    @ResponseBody
    public ResultVo getRecentlyBorrowInfo(){
        Subject subject = SecurityUtils.getSubject();
        User loginUser = (User) subject.getPrincipal();
        List<BorrowInfo> borrowInfos = new ArrayList<>();
        List<Role> roles = roleDao.selectRoleByUserId(loginUser.getId());
        Boolean selectAllFlag = false;
        if(roles!=null){
            for (Role role:roles) {
                if(role.getRoleName().equals("管理员")){
                    selectAllFlag = true;
                    break;
                }
            }
        }else{
            return ResultVo.build(201,"");
        }
        if(selectAllFlag==true){
            borrowInfos = borrowInfoDao.selectBorrowInfo();
        }else {
            borrowInfos = borrowInfoDao.selectBorrowInfoByBorrowPersonId(loginUser.getId());
        }
        List<BorrowInfoVo> borrowInfoVos = new ArrayList<>();
        if(null!=borrowInfos){
            for (BorrowInfo borrowInfo:borrowInfos) {
                BorrowInfoVo borrowInfoVo = new BorrowInfoVo();
                borrowInfoVo.setId(borrowInfo.getId());
                borrowInfoVo.setBorrowNum(borrowInfo.getBorrowNum());
                borrowInfoVo.setBorrowTime(borrowInfo.getBorrowTime());
                borrowInfoVo.setReturnTime(borrowInfo.getReturnTime());
                borrowInfoVo.setBorrowHandleState(borrowInfo.getBorrowHandleState());
                String borrowPerson = userDao.selectUserNameById(borrowInfo.getBorrowPersonId());
                borrowInfoVo.setBorrowPerson(borrowPerson);
                String borrowGoods = goodsDao.selectGoodsNameById(borrowInfo.getBorrowGoodsId());
                borrowInfoVo.setBorrowGoods(borrowGoods);
                borrowInfoVo.setModifyPerson(borrowInfo.getModifyPerson());
                borrowInfoVos.add(borrowInfoVo);
            }
            return ResultVo.build(borrowInfoVos.size(),0,"",borrowInfoVos);
        }else {
            return ResultVo.build(201,"");
        }
    }
}
